# Copyright (C) 2006,2007  Stephane Alnet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

#
# For more information visit http://carrierclass.net/
#

use strict; use warnings;

1;
=pod
# First step in adding a new server to the system.
'add-server' => {
    entry_content => [ 
        ip          => [ 'New Server IP',   'server_interface.ipnet_ip',    1 ],
        login       => [ 'Web Login',       'ui_user.login',                1 ],
        password    => [ 'Web Password',    'ui_user.password',             1 ],
        system      => [ 'Add to system',   'system.pk',                    1 ],
    ],
    # Note: the first parameter in run() is always a Framework::Controller
    run => sub {
        my ($self,$web) = @_;
        my %values = $self->retrieve_values($web);

        use CCN::UIDB;
        my $manager_pk = CCN::UIDB::db->sql_get('SELECT manager FROM systems WHERE id = ?',$values{system});

        use CCNCore::Provision::Distributed;
        my $b = new CCNCore::Provision::Distributed(CCN::UIDB::provisioning());

        use CCN::IPC::Ticket;
        my $t = new CCN::IPC::Ticket;
        $t->set_label('Adding remote server at IP '.$values{ip});
        $t->add_action(1,'add-server-completed 0');
        # The system, once created (in add-server-completed) will belong to the specified
        # system and be owned by the user who added it.
        $t->add_context('system_pk',$values{system});
        $t->add_context('owner_pk',$web->login);
        $t->add_context('manager_pk',$manager_pk);

        use CCN::Text;
        # YYY Correct this (cleartext)
        my $login    = CCN::Text::text_to_hex($values{login});
        my $password = CCN::Text::text_to_hex($values{password});

        $b->spool_this($manager_pk,$t->ticket,"add-server $values{ip} $login $password");
        return "<h2>Server is been queried</h2>"; # Should return the status page for the ticket.
    }
},
=cut